﻿using System.Text.RegularExpressions;
using RAP.Framework.Libary.Utils;

using System.Collections;
using RAP.Framework.Libary.DomTemplateBuilder.Element.Collection;

using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler
{
	public class RegexMatchesHandler : IterateHandler
	{
		/// <summary>
		/// 
		/// </summary>
		public new virtual RegexMatches Element
		{
			get
			{
				return base.Element as RegexMatches;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="paramObject"></param>
		/// <returns></returns>
		public override object GetPropertyValue ( object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			var value = base.GetPropertyValue ( rootParamObject, parentParamObject, context );
			if ( value == null )
			{
				return null;
			}
			var expression = this.Element.Expression;
			if ( expression.IsNullOrEmpty ( ) )
			{
				return null;
			}
			var matches = Regex.Matches ( value.ToString ( ), expression, RegexOptions.Multiline );
			var result = new ArrayList ( );
			foreach ( Match match in matches )
			{
				result.Add ( match.Value );
			}
			return result;
		}
	}
}
